/**
 * @Title: drugStoreApproveList_ctrl.js
 * @Description: TODO
 * Copyright: Copyright (c) 2015
 * Company:中国平安
 *
 * @Project HIMS-FWA
 * @author EX-CHENLIN002
 * @date 2015年2月12日 上午11:29:35
 * @version V1.0
 */
;
(function(){
	'use strict';
	
	var app = angular.module('fwa.mainApp');
	app.controller('DrugStoreApproveCtrl',['$scope','$modal','BaseURL','Modal','DrugStoreService','ApproveDrugStoreService','CommunityService','$state',
	                             function($scope,$modal, baseUrl, Modal, DrugStoreService,ApproveDrugStoreService,CommunityService,$state) {
		//$scope.drugStoreService = DrugStoreService;
		//DrugStoreService.initMedicalOrgInfoList();
		$scope.communityService = CommunityService;
		CommunityService.initPharmacyDatas();
		//查询条件 对象
		$scope.form = {};
		
		//分页配置
		$scope.current = {
							currentPage: 0,  
							limit: 20
						 };
		//表头
		$scope.columns = [];
		
		$scope.evaluationScroes = [
		                           {"name":"≥95分","code":"95"},
		                           {"name":"≥76分","code":"76"}
		                          ];
		var maps = ['one','two','three','four'];
		//获取表头考评得分
		function getTheadScore() {
			var params = getParams();
			DrugStoreService.getEvaluationName(params).success(function (res) {
				var width = 80;
				if (res.list && res.list.length > 0) {
					width = 80/res.list.length;
				}
				var columns = [{label:"合计",map:"total",width:"20%"}];
				angular.forEach(res.list,function(data,i) {
					var column = {};
					column.label = data;
					column.map = maps[i];
					column.width = width;
					columns.push(column);
				});
				$scope.columns = columns;
			});
		}
		
		$scope.form = {};
		$scope.loading = false;
		
		//获取参数
		function getParams() {
			var params = {
					currentPage: $scope.current.currentPage,
					limit: $scope.current.limit,
					orgName: $scope.form.orgName,
					evaluationYear: $scope.form.evaluationYear,
					bedName: $scope.form.bedName,
					evaluationScroe: $scope.form.evaluationScroe
				};
			return params;
		}
		
		/**
		 * 考评操作
		 */
		$scope.operation=function (data){
			var status=data.status;
			if (status =='6') { //已经完成了
				$state.go('drugStoreIntgrityManager.finishDrugStore',{evaluationCode:data.evaluationCode});
			}
			else if (status == '5') {//待审批
				$state.go('drugStoreApproveManager.drugStoreOrgApprove',{evaluationCode:data.evaluationCode});
			}
		}
		$scope.loading = false;
		
		//查询
		function query() {
			var params = getParams();
			if(!$scope.form.evaluationYear){
				Modal.alert("考评年度必录！");
				return ;
			}
			params.processStatus=[5,6]; //待审批
			params.evalautionType = '0';	//考评类别：0：机构，1,药店
			$scope.loading = true;
			DrugStoreService.getEvaluationOrgList(params).success(function(res){
				$scope.tableData = res.orgList;
				$scope.totalCount = res.orgCount;
				$scope.loading = false;
			}).error(function(e){
				$scope.loading = false;
				Modal.alert("error");
			});
			
			getTheadScore();
		}
		
		//查询
		$scope.onSubmit = function() {
			$scope.current.currentPage = 1;
			query();
		}
		
		//重置
		$scope.onReset = function() {
			$scope.form = {};
		}
		
		//分页监听
		$scope.$on('pageTool.skipPage',function(e,v){
			$scope.current.currentPage = v;
			query();
		});
		//check add
		//存储选中的集合
		var selectDataAll = [];
		
		//表头 checkbox 是否选中 默认不选中
		$scope.checked = false;
		
		function findSelectData(data) {
			angular.forEach(selectDataAll,function(selectData,i) {
				if (selectData.evaluationCode == data.evaluationCode) {
					return i;
				}
			});
			return -1;
		}
		
		//checkbox 全选以及全不选
		$scope.selectAll = function(checked) {
			angular.forEach($scope.tableData,function(data,i) {
				if (checked) {
					var index = findSelectData(data);
					if (index == -1) {
						selectDataAll.push(data);
					}
				}
				else {
					selectDataAll = []
				}
				data.checked = checked;
				
			});
		}
		
		//checkbox 单选
		$scope.selectData = function (data) {
			if (data.checked) {
				var index = findSelectData(data);
				if (index == -1) {
					selectDataAll.push(data);
				}
			}
			else {
				var index = findSelectData(data);
				if (index == -1) {
					selectDataAll.splice(index,1);
				}
			}
			var length = $scope.tableData.length;
			var selectNum = 0;
			angular.forEach($scope.tableData,function(data,i){
				if (data.checked) {
					selectNum++;
				}
			});
			if (selectNum < length) {
				$scope.checked = false;
			}
			else {
				$scope.checked = true;
			}
		}
		//right end
		/**
	     * 审核批量提交
	     */
		$scope.batchSumit = function() {
			if (selectDataAll && selectDataAll != '') {
				var isFlag = false;
				angular.forEach(selectDataAll,function(data,i) {
					if (data.status != '5') {
						isFlag = true;
					}
				});
				if (isFlag) {
					Modal.alert("只有审批才能批量操作!!!");
					return false;
				}
				else {
					popDialogFun();
				}
			}
			else {
				Modal.alert("请勾选要批量提交的记录!!!");
				return false;
			} 
		}
		//
		/**
	      * 批量操作弹出框
	      */
	     function popDialogFun() {
	 		var modalInstance = $modal.open({
	 		      templateUrl:'batchSubmit.html',
	 		      backdrop:'static',
	 			  size:'sm',
	 			  controller: function($scope, $http,$modalInstance,Modal) {
	 				 
	 				$scope.form = {};
	 				$scope.checkList = [{"code":"1","name":"同意"},{"code":"0","name":"不同意"}];
	 				
	 				$scope.batchConfirm = function() {
	 					if ($scope.form.auditOpinion) {
	 						if ($scope.form.auditOpinion == '1' && !$scope.form.auditDesc) {
	 							Modal.alert("意见描述不能为空!!!");
		 						return false;
	 						}
	 						else {
	 							var evaluationCodes = [];
	 							angular.forEach(selectDataAll,function(data,i){
	 								evaluationCodes.push(data.evaluationCode);
	 							});
	 							var data = {auditOpinion:$scope.form.auditOpinion,auditDesc:$scope.form.auditDesc || '',evaluationCodes:evaluationCodes};
	 							$scope.checked = true;
	 							ApproveDrugStoreService.reviewBatchSumit(data).success(function(res) {
	 								Modal.alert(res.msg);
	 								if (res.state == 0) {
	 									 $scope.onClose();
	 									$scope.onSubmit();
	 								}
	 								$scope.checked = false;
	 							});
	 						}
	 					}
	 					else {
	 						Modal.alert("审批意见不能为空!!!");
	 						return false;
	 					}
	 				}
	 				  //关闭
	 				 $scope.onClose = function () {
	 					$modalInstance.dismiss();
	 				 }
	 			  }
	 		 });
	 	 }
		//check add end 
		
	   //初始化年份
	   CommunityService.initDate("yyyy").success(function(res){
			$scope.form.evaluationYear = res.formatDate;
			//页面加载时调用
			$scope.onSubmit();
	   });
		
	}]);
})();